import 'dart:async';
void main() async {
  print('Start');
  await checkVersion();
  print('End');


// 调用同步函数
  print('Sync Version: ${syncLookUpVersion()}');

  // 调用异步函数
  print('Fetching async version...');
  var version = await asyncLookUpVersion();
  print('Async Version: $version');




//https://dart.cn/language/async/#handling-streams
// 创建一个 Stream，每秒发出一个数字
  final numberStream = Stream.periodic(Duration(seconds: 1), (i) => i + 1);

  print('Starting to listen to the stream...');

  // 使用 await for 处理 Stream
  await for (final number in numberStream) {
    print('Received number: $number');
    if (number >= 5) {
      print('Stopping after receiving 5 numbers.');
      break;
    }
  }

  print('Finished listening to the stream.');

}


//https://dart.cn/language/async/#handling-futures
// 模拟一个异步函数
Future<String> lookUpVersion() async {
  await Future.delayed(Duration(seconds: 2)); // 模拟耗时操作
  return '1.0.0';
}

Future<void> checkVersion() async {
  try {
    var version = await lookUpVersion();
    print('Version: $version');
  } catch (e) {
    print('Error: $e');
  }
}






//https://dart.cn/language/async/#declaring-async-functions
// 同步函数
String syncLookUpVersion() => '1.0.0';

// 异步函数
Future<String> asyncLookUpVersion() async {
  await Future.delayed(Duration(seconds: 2)); // 模拟耗时操作
  return '1.0.0';
}

